package com.swingson.properties.specificclass_impl;

import com.swingson.SwingsonGui;
import com.swingson.event.ValueEvent;
import com.swingson.jsonparser.JsonElement;
import com.swingson.other.ConditionAndAction;
import com.swingson.other.MethodExecutor;
import com.swingson.properties.AbstractAssignMethodPropertyProcessor;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JComponent;

/**
 * @author sad
 */
public class OnUnhandledExceptionPropertyProcessor extends AbstractAssignMethodPropertyProcessor {

    static List<Class[]> argumentsVariants = new ArrayList<Class[]>() {
        {
            add(new Class[]{Throwable.class});
        }
    };

    @Override
    protected String getName() {
        return "Unhandled Exception Event Listener";
    }

    @Override
    protected List<Class[]> methodArgumentsVariants() {
        return argumentsVariants;
    }

    @Override
    protected void attachMethod(JsonElement jsonElement, JComponent component, final SwingsonGui gui, String propertyName, final MethodExecutor mExecutor, final int methodArgumentVariant, ConditionAndAction conditionAndAction) {
        gui.addExceptionHandler(new ValueEvent<Throwable>() {
            @Override
            protected void valueChanged(Throwable value) {
                try {
                    switch (methodArgumentVariant) {
                        case 0:
                            mExecutor.invoke(value);
                            break;
                    }
                } catch (Throwable ex) {
                    onError(gui, ex);
                }
            }
        });
    }
}
